import os
import json
from datetime import datetime

class Config:
    def __init__(self, config_path='config.json'):
        self.config_path = config_path
        self.default_config = {
            'data_path': 'data/1220电量&月份&用户数.xlsx',
            'last_modified': None,
            'best_method': {
                'preprocess': 'pre_replace',
                'feature': 'seasonal'
            },
            'last_method': {
                'preprocess': None,
                'feature': None
            }
        }
        self.load_config()
    
    def load_config(self):
        """加载配置文件"""
        if os.path.exists(self.config_path):
            with open(self.config_path, 'r', encoding='utf-8') as f:
                self.config = json.load(f)
        else:
            self.config = self.default_config
            self.save_config()
    
    def save_config(self):
        """保存配置文件"""
        with open(self.config_path, 'w', encoding='utf-8') as f:
            json.dump(self.config, f, indent=4, ensure_ascii=False)
    
    def get_data_modified_time(self):
        """获取数据文件修改时间"""
        data_path = os.path.join(os.path.dirname(__file__), self.config['data_path'])
        if os.path.exists(data_path):
            return datetime.fromtimestamp(os.path.getmtime(data_path)).strftime('%Y-%m-%d %H:%M:%S')
        return None
    
    def is_data_modified(self):
        """检查数据是否被修改"""
        current_modified = self.get_data_modified_time()
        stored_modified = self.config['last_modified']
        
        if current_modified != stored_modified:
            return True
        return False
    
    def update_last_modified(self):
        """更新最后修改时间"""
        self.config['last_modified'] = self.get_data_modified_time()
        self.save_config()
    
    def update_best_method(self, preprocess_method, feature_method):
        """更新最佳方法"""
        self.config['best_method'] = {
            'preprocess': preprocess_method,
            'feature': feature_method
        }
        self.save_config()
    
    def update_last_method(self, preprocess_method, feature_method):
        """更新上次使用的方法"""
        self.config['last_method'] = {
            'preprocess': preprocess_method,
            'feature': feature_method
        }
        self.save_config() 